<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li class="active"><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 初始化项目">1. 初始化项目</a></li><li><a href="#t12. 基本计数器">2. 基本计数器</a><ul><li><a href="#t22.1 使用dva">2.1 使用dva</a></li><li><a href="#t32.2 实现dva">2.2 实现dva</a></li></ul></li><li><a href="#t43. 支持effects">3. 支持effects</a><ul><li><a href="#t53.1 使用">3.1 使用</a></li><li><a href="#t63.2 实现">3.2 实现</a></li></ul></li><li><a href="#t74.支持路由">4.支持路由</a><ul><li><a href="#t84.1 使用">4.1 使用</a></li><li><a href="#t94.2 index.js">4.2 index.js</a></li><li><a href="#t104.3 router.js">4.3 router.js</a></li></ul></li><li><a href="#t115.省略命名空间">5.省略命名空间</a></li><li><a href="#t12参考">参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;">1. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE; <a href="#t01. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;"> # </a></h2>
<pre><code class="lang-js">create-react-app zhufeng-dva-source
cd  zhufeng-dva-source
yarn add redux react-redux react-router-dom react-router-redux redux history
yarn add dva
yarn start
</code></pre>
<h2 id="t12. &#x57FA;&#x672C;&#x8BA1;&#x6570;&#x5668;">2. &#x57FA;&#x672C;&#x8BA1;&#x6570;&#x5668; <a href="#t12. &#x57FA;&#x672C;&#x8BA1;&#x6570;&#x5668;"> # </a></h2>
<h3 id="t22.1 &#x4F7F;&#x7528;dva">2.1 &#x4F7F;&#x7528;dva <a href="#t22.1 &#x4F7F;&#x7528;dva"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> dva,{connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;dva&apos;</span>;

<span class="hljs-comment">// 1. &#x521D;&#x59CB;&#x5316;</span>
<span class="hljs-keyword">const</span> app = dva();

<span class="hljs-comment">// 2. &#x5B9A;&#x4E49;&#x6A21;&#x578B;</span>
app.model({
  <span class="hljs-attr">namespace</span>: <span class="hljs-string">&apos;count&apos;</span>,
  <span class="hljs-attr">state</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">reducers</span>: {
    add  (count) { <span class="hljs-keyword">return</span> count + <span class="hljs-number">1</span> }
  },
});

<span class="hljs-comment">// 3. View</span>
<span class="hljs-keyword">const</span> App = connect(<span class="hljs-function">(<span class="hljs-params">{ count }</span>) =&gt;</span> ({
  count
}))(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>{ props.count }<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> { props.dispatch({type: &apos;count/add&apos;})}}&gt;add<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
});

<span class="hljs-comment">// 4. Router</span>
app.router(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> &lt;App /&gt;);

<span class="hljs-comment">// 5. Start</span>
app.start(<span class="hljs-string">&apos;#root&apos;</span>);
</code></pre>
<h3 id="t32.2 &#x5B9E;&#x73B0;dva">2.2 &#x5B9E;&#x73B0;dva <a href="#t32.2 &#x5B9E;&#x73B0;dva"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {combineReducers,createStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">export</span> {
    connect
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> app={
        <span class="hljs-attr">_models</span>: [],
        <span class="hljs-attr">_store</span>:<span class="hljs-literal">null</span>,
        model,
        <span class="hljs-attr">_router</span>:<span class="hljs-literal">null</span>,
        router,
        start
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">router</span>(<span class="hljs-params">router</span>) </span>{
        app._router=router;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">model</span>(<span class="hljs-params">m</span>) </span>{
        app._models.push(m);
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">start</span>(<span class="hljs-params">root</span>) </span>{
        <span class="hljs-keyword">let</span> reducers=app._models.reduce(<span class="hljs-function">(<span class="hljs-params">memo,model</span>) =&gt;</span> {
            memo[model.namespace]=<span class="hljs-function">(<span class="hljs-params">state=model.state,action</span>) =&gt;</span> {
                <span class="hljs-keyword">let</span> actionType = action.type;
                <span class="hljs-keyword">let</span> values=actionType.split(<span class="hljs-string">&apos;/&apos;</span>);
                <span class="hljs-keyword">if</span> (values.length ==<span class="hljs-number">2</span> &amp;&amp; values[<span class="hljs-number">0</span>] == model.namespace) {
                    <span class="hljs-keyword">let</span> reducer=model.reducers[values[<span class="hljs-number">1</span>]];
                    <span class="hljs-keyword">if</span> (reducer) {
                        <span class="hljs-keyword">return</span> reducer(state,action);
                    }
                }
                <span class="hljs-keyword">return</span> state;
            }
            <span class="hljs-keyword">return</span> memo;
        },{});
        <span class="hljs-keyword">const</span> reducer=combineReducers(reducers);
        <span class="hljs-keyword">const</span> store=app._store=createStore(reducer);
        <span class="hljs-keyword">let</span> App=app._router;
        ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">App</span>/&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>,<span class="hljs-built_in">document</span>.querySelector(root));
    }
    <span class="hljs-keyword">return</span> app;
}
</code></pre>
<h2 id="t43. &#x652F;&#x6301;effects">3. &#x652F;&#x6301;effects <a href="#t43. &#x652F;&#x6301;effects"> # </a></h2>
<h3 id="t53.1 &#x4F7F;&#x7528;">3.1 &#x4F7F;&#x7528; <a href="#t53.1 &#x4F7F;&#x7528;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> dva,{connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./dva&apos;</span>;

<span class="hljs-comment">// 1. &#x521D;&#x59CB;&#x5316;</span>
<span class="hljs-keyword">const</span> app = dva();
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">delay</span>(<span class="hljs-params">ms</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve,reject</span>) =&gt;</span> {
        setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            resolve();
        },ms);
    });
}

<span class="hljs-comment">// 2. &#x5B9A;&#x4E49;&#x6A21;&#x578B;</span>
app.model({
  <span class="hljs-attr">namespace</span>: <span class="hljs-string">&apos;count&apos;</span>,
  <span class="hljs-attr">state</span>: <span class="hljs-number">0</span>,
  <span class="hljs-attr">reducers</span>: {
    add  (count) { <span class="hljs-keyword">return</span> count + <span class="hljs-number">1</span> }
  },
  <span class="hljs-attr">effects</span>: {
        *asyncAdd({call,put}) {
            <span class="hljs-keyword">yield</span> call(delay, <span class="hljs-number">1000</span>);
            <span class="hljs-keyword">yield</span> put({ <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;count/add&apos;</span> });
        }
  }
});

<span class="hljs-comment">// 3. View</span>
<span class="hljs-keyword">const</span> App = connect(<span class="hljs-function">(<span class="hljs-params">{ count }</span>) =&gt;</span> ({
  count
}))(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">props</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>{ props.count }<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;add&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> {props.dispatch({type: &apos;count/add&apos;})}}&gt;add<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">key</span>=<span class="hljs-string">&quot;asyncAdd&quot;</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> { props.dispatch({type: &apos;count/asyncAdd&apos;})}}&gt;async add<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
});

<span class="hljs-comment">// 4. Router</span>
app.router(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> &lt;App /&gt;);

<span class="hljs-comment">// 5. Start</span>
app.start(<span class="hljs-string">&apos;#root&apos;</span>);;

</code></pre>
<h3 id="t63.2 &#x5B9E;&#x73B0;">3.2 &#x5B9E;&#x73B0; <a href="#t63.2 &#x5B9E;&#x73B0;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {combineReducers,createStore, applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> createSagaMiddleware <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-saga&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> effects <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-saga/effects&apos;</span>;

<span class="hljs-keyword">export</span> {
    connect
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> app={
        <span class="hljs-attr">_models</span>: [],
        <span class="hljs-attr">_store</span>:<span class="hljs-literal">null</span>,
        model,
        <span class="hljs-attr">_router</span>:<span class="hljs-literal">null</span>,
        router,
        start
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">router</span>(<span class="hljs-params">router</span>) </span>{
        app._router=router;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">model</span>(<span class="hljs-params">m</span>) </span>{
        app._models.push(m);
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">start</span>(<span class="hljs-params">root</span>) </span>{
        <span class="hljs-keyword">let</span> reducers=app._models.reduce(<span class="hljs-function">(<span class="hljs-params">memo,model</span>) =&gt;</span> {
            memo[model.namespace]=<span class="hljs-function">(<span class="hljs-params">state=model.state,action</span>) =&gt;</span> {
                <span class="hljs-keyword">let</span> actionType = action.type;
                <span class="hljs-keyword">let</span> values=actionType.split(<span class="hljs-string">&apos;/&apos;</span>);
                <span class="hljs-keyword">if</span> (values.length ==<span class="hljs-number">2</span> &amp;&amp; values[<span class="hljs-number">0</span>] == model.namespace) {
                    <span class="hljs-keyword">let</span> reducer=model.reducers[values[<span class="hljs-number">1</span>]];
                    <span class="hljs-keyword">if</span> (reducer) {
                        <span class="hljs-keyword">return</span> reducer(state,action);
                    }
                }
                <span class="hljs-keyword">return</span> state;
            }
            <span class="hljs-keyword">return</span> memo;
        },{});
        <span class="hljs-keyword">const</span> reducer=combineReducers(reducers);
        <span class="hljs-keyword">const</span> sagaMiddleware=createSagaMiddleware();
        <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">rootSaga</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> m <span class="hljs-keyword">of</span> app._models) {
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> m.effects) {
                    <span class="hljs-keyword">yield</span> effects.takeEvery(m.namespace+<span class="hljs-string">&apos;/&apos;</span>+key,m.effects[key],effects);
                }
            }
        }
        <span class="hljs-keyword">const</span> store=app._store=createStore(reducer,applyMiddleware(sagaMiddleware));
        sagaMiddleware.run(rootSaga);
        <span class="hljs-keyword">let</span> App=app._router;
        ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">App</span>/&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>,<span class="hljs-built_in">document</span>.querySelector(root));
    }
    <span class="hljs-keyword">return</span> app;
}
</code></pre>
<h2 id="t74.&#x652F;&#x6301;&#x8DEF;&#x7531;">4.&#x652F;&#x6301;&#x8DEF;&#x7531; <a href="#t74.&#x652F;&#x6301;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t84.1 &#x4F7F;&#x7528;">4.1 &#x4F7F;&#x7528; <a href="#t84.1 &#x4F7F;&#x7528;"> # </a></h3>
<pre><code class="lang-js">import React,{Fragment} from &apos;react&apos;;
import dva,{connect} from &apos;./dva&apos;;
import {Router,Route} from &apos;dva/router&apos;;
const app = dva();
function delay(ms) {
    return new Promise((resolve,reject) =&gt; {
        setTimeout(function () {
            resolve();
        },ms);
    });
}

app.model({
  namespace: &apos;count&apos;,
  state: 0,
  reducers: {
    add  (count) { return count + 1 }
  },
  effects: {
        *asyncAdd({call,put}) {
            yield call(delay, 1000);
            yield put({ type: &apos;add&apos; });
        }
  }
});


const App = connect(({ count }) =&gt; ({
  count
}))(function(props) {
  return (
    &lt;div&gt;
      &lt;h2&gt;{ props.count }&lt;/h2&gt;
          &lt;button key=&quot;add&quot; onClick={() =&gt; {props.dispatch({type: &apos;count/add&apos;})}}&gt;add&lt;/button&gt;
          &lt;button key=&quot;asyncAdd&quot; onClick={() =&gt; { props.dispatch({type: &apos;count/asyncAdd&apos;})}}&gt;async add&lt;/button&gt;
 &lt;/div&gt;
  );
});

const Home=() =&gt; &lt;div&gt;Home&lt;/div&gt;

app.router(({history}) =&gt; (
    &lt;Router history={history}&gt;
       &lt;Fragment&gt;
         &lt;Route path=&quot;/&quot; component={Home} /&gt;
         &lt;Route path=&quot;/counter&quot; component={App}/&gt;
       &lt;/Fragment&gt;
    &lt;/Router&gt;
));

app.start(&apos;#root&apos;);
</code></pre>
<h3 id="t94.2 index.js">4.2 index.js <a href="#t94.2 index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {
    combineReducers,createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> createSagaMiddleware <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-saga&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> effects <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-saga/effects&apos;</span>;
<span class="hljs-keyword">import</span> createHashHistory <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;history/createHashHistory&apos;</span>;

<span class="hljs-keyword">export</span> {
    connect
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> app={
        <span class="hljs-attr">_models</span>: [],
        <span class="hljs-attr">_store</span>:<span class="hljs-literal">null</span>,
        model,
        <span class="hljs-attr">_router</span>:<span class="hljs-literal">null</span>,
        router,
        start
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">router</span>(<span class="hljs-params">router</span>) </span>{
        app._router=router;
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">model</span>(<span class="hljs-params">m</span>) </span>{
        app._models.push(m);
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">start</span>(<span class="hljs-params">root</span>) </span>{
        <span class="hljs-keyword">let</span> reducers=app._models.reduce(<span class="hljs-function">(<span class="hljs-params">memo,model</span>) =&gt;</span> {
            memo[model.namespace]=<span class="hljs-function">(<span class="hljs-params">state=model.state,action</span>) =&gt;</span> {
                <span class="hljs-keyword">let</span> actionType = action.type;
                <span class="hljs-keyword">let</span> values=actionType.split(<span class="hljs-string">&apos;/&apos;</span>);
                <span class="hljs-keyword">if</span> (values.length ==<span class="hljs-number">2</span> &amp;&amp; values[<span class="hljs-number">0</span>] == model.namespace) {
                    <span class="hljs-keyword">let</span> reducer=model.reducers[values[<span class="hljs-number">1</span>]];
                    <span class="hljs-keyword">if</span> (reducer) {
                        <span class="hljs-keyword">return</span> reducer(state,action);
                    }
                }
                <span class="hljs-keyword">return</span> state;
            }
            <span class="hljs-keyword">return</span> memo;
        },{});
        <span class="hljs-keyword">const</span> reducer=combineReducers(reducers);
        <span class="hljs-keyword">const</span> sagaMiddleware=createSagaMiddleware();
        <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">rootSaga</span>(<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> m <span class="hljs-keyword">of</span> app._models) {
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> m.effects) {
                    <span class="hljs-keyword">yield</span> effects.takeEvery(m.namespace+<span class="hljs-string">&apos;/&apos;</span>+key,m.effects[key],effects);
                }
            }
        }
        <span class="hljs-keyword">let</span> history=createHashHistory();
        <span class="hljs-keyword">const</span> store=app._store=createStore(reducer,applyMiddleware(sagaMiddleware));
        sagaMiddleware.run(rootSaga);
        <span class="hljs-keyword">let</span> App=app._router({history});
        ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
            {App}
        <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>,<span class="hljs-built_in">document</span>.querySelector(root));
    }
    <span class="hljs-keyword">return</span> app;
}
</code></pre>
<h3 id="t104.3 router.js">4.3 router.js <a href="#t104.3 router.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {Router,Route} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">export</span> {
    Router,Route
}
</code></pre>
<h2 id="t115.&#x7701;&#x7565;&#x547D;&#x540D;&#x7A7A;&#x95F4;">5.&#x7701;&#x7565;&#x547D;&#x540D;&#x7A7A;&#x95F4; <a href="#t115.&#x7701;&#x7565;&#x547D;&#x540D;&#x7A7A;&#x95F4;"> # </a></h2>
<pre><code class="lang-js"> <span class="hljs-keyword">let</span> reducers=app._models.reduce(<span class="hljs-function">(<span class="hljs-params">memo,model</span>) =&gt;</span> {
            memo[model.namespace]=<span class="hljs-function">(<span class="hljs-params">state=model.state,action</span>) =&gt;</span> {
                <span class="hljs-keyword">let</span> actionType = action.type;
                <span class="hljs-keyword">let</span> [namespace,type]=actionType.split(<span class="hljs-string">&apos;/&apos;</span>);
                <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> type == <span class="hljs-string">&apos;undefined&apos;</span>) {
                    type=namespace;
                    namespace=<span class="hljs-literal">undefined</span>;
                }
                <span class="hljs-keyword">if</span> (!namespace|| namespace== model.namespace) {
                    <span class="hljs-keyword">let</span> reducer=model.reducers[type];
                    <span class="hljs-keyword">if</span> (reducer) {
                        <span class="hljs-keyword">return</span> reducer(state,action);
                    }
                }
                <span class="hljs-keyword">return</span> state;
            }
            <span class="hljs-keyword">return</span> memo;
        },{});
</code></pre>
<h2 id="t12&#x53C2;&#x8003;">&#x53C2;&#x8003; <a href="#t12&#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://gitee.com/zhufengpeixun/zhufeng-dva-source.git">zhufeng-dva-source</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
